unit RequestEntry;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzBmpBtn, RzRadChk, Grids, BaseGrid, AdvGrid, DBAdvGrd,
  StdCtrls, Mask, RzEdit, RzSpnEdt, RzTabs, RzButton, ExtCtrls, RzPanel,
  DB, ADODB, RzCmboBx, RzBorder, RzLabel, ComCtrls, RzDTP, RzDBCmbo,
  DBCtrls, RzDBDTP, RzDBEdit;

type
  TfrmRequestEntry = class(TForm)
    pnlMainTop: TRzPanel;
    btnMainClose: TRzBitBtn;
    RzPanel2: TRzPanel;
    pnlMainClient: TRzPanel;
    MainPageRequestEntry: TRzPageControl;
    TabSheet1: TRzTabSheet;
    RzPanel3: TRzPanel;
    RzPanel4: TRzPanel;
    spinItemSelect: TRzSpinButtons;
    edtActiveItem: TRzEdit;
    edtFindItem: TRzEdit;
    RzBitBtn1: TRzBitBtn;
    RzBitBtn2: TRzBitBtn;
    RzBitBtn3: TRzBitBtn;
    dbgridRequestItems: TDBAdvStringGrid;
    TabSheet2: TRzTabSheet;
    RzPanel35: TRzPanel;
    RzPanel36: TRzPanel;
    btnSubjectRemove: TRzBitBtn;
    btnSubjectAdd: TRzBitBtn;
    btnSubjectSearch: TRzBitBtn;
    edtSearchItem: TRzEdit;
    spineditSubject: TRzSpinButtons;
    edtCurrentNameEmp: TRzEdit;
    cbMainFilterNavbar: TRzCheckBox;
    dbgridRequestDetails: TDBAdvStringGrid;
    rzBtnDTRSummary: TRzBmpButton;
    RzBmpButton1: TRzBmpButton;
    ADOConnection1: TADOConnection;
    TabSheet3: TRzTabSheet;
    QryRequestDetails: TADODataSet;
    dsRequestDetails: TDataSource;
    QryItem: TADODataSet;
    dsQryItem: TDataSource;
    btnAddtoItem: TRzBitBtn;
    RzLabel1: TRzLabel;
    RzBorder1: TRzBorder;
    RzLabel2: TRzLabel;
    im_edtName: TRzEdit;
    im_edtDescription: TRzEdit;
    RzLabel3: TRzLabel;
    im_cb_Supplier: TRzComboBox;
    RzLabel4: TRzLabel;
    im_cb_Category: TRzComboBox;
    RzLabel5: TRzLabel;
    im_edtPrice: TRzNumericEdit;
    RzLabel6: TRzLabel;
    RzLabel7: TRzLabel;
    im_cb_unit: TRzComboBox;
    RzBorder2: TRzBorder;
    RzBitBtn5: TRzBitBtn;
    RzBitBtn6: TRzBitBtn;
    TabSheet4: TRzTabSheet;
    RzPanel1: TRzPanel;
    RzLabel8: TRzLabel;
    RzLabel9: TRzLabel;
    RzLabel10: TRzLabel;
    RzLabel11: TRzLabel;
    RzLabel12: TRzLabel;
    RzLabel13: TRzLabel;
    RzLabel14: TRzLabel;
    RzLabel15: TRzLabel;
    RzLabel16: TRzLabel;
    RzDBEdit1: TRzDBEdit;
    RzDBDateTimePicker1: TRzDBDateTimePicker;
    RzDBDateTimePicker2: TRzDBDateTimePicker;
    RzDBEdit2: TRzDBEdit;
    RzDBLookupComboBox1: TRzDBLookupComboBox;
    RzDBLookupComboBox2: TRzDBLookupComboBox;
    RzDBLookupComboBox3: TRzDBLookupComboBox;
    RzDBLookupComboBox4: TRzDBLookupComboBox;
    RzDBComboBox1: TRzDBComboBox;
    QryRequest: TADODataSet;
    dsQryRequest: TDataSource;
    QryEmployee: TADODataSet;
    dsQryEmployee: TDataSource;
    procedure FormShow(Sender: TObject);
    procedure RzBitBtn1Click(Sender: TObject);
    procedure RzBitBtn5Click(Sender: TObject);
    procedure spinItemSelectDownLeftClick(Sender: TObject);
    procedure spinItemSelectUpRightClick(Sender: TObject);
    procedure RzBitBtn6Click(Sender: TObject);
    procedure RzBitBtn2Click(Sender: TObject);
    procedure btnAddtoItemClick(Sender: TObject);
    procedure RzBitBtn3Click(Sender: TObject);
    procedure rzBtnDTRSummaryClick(Sender: TObject);
    procedure RzBmpButton1Click(Sender: TObject);
    procedure btnMainCloseClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmRequestEntry: TfrmRequestEntry;

implementation

{$R *.dfm}

procedure TfrmRequestEntry.FormShow(Sender: TObject);
begin
     QryRequestDetails.Connection:=ADOConnection1;
     QryRequestDetails.Open;

     QryItem.Connection:=ADOConnection1;
     QryItem.Open;

     QryEmployee.Connection:=ADOConnection1;
     QryEmployee.Open;

     MainPageRequestEntry.HideAllTabs;
     MainPageRequestEntry.ActivePageIndex:=0;     

end;

procedure TfrmRequestEntry.RzBitBtn1Click(Sender: TObject);
begin
     if trim(edtFindItem.Text)='' then
     begin
          edtFindItem.SetFocus;
          exit;
     end;

     QryItem.Filter:='Name like ''%' + trim(edtFindItem.Text) + '%''';
     QryItem.Filtered:=True;

     if QryItem.IsEmpty then
     begin
          edtFindItem.Color:=clRed;
          edtFindItem.SetFocus;
          btnAddtoItem.Visible:=True;
     end else
     begin
         edtFindItem.Color:=clCream;
         spinItemSelect.SetFocus;
     end;
end;

procedure TfrmRequestEntry.RzBitBtn5Click(Sender: TObject);
begin

    if trim(im_edtName.Text)='' then
    begin
    im_edtName.SetFocus;
    Exit;
    end;
    if trim(im_edtDescription.Text)='' then
    begin
    im_edtDescription.SetFocus;
    Exit;
    end;

     with  QryItem do
     begin
          Append;
          FieldByName('Name').AsString:=trim(im_edtName.Text);
          FieldByName('Description').AsString:=trim(im_edtDescription.Text);
          FieldByName('SupplierKey').AsInteger:=1;
          FieldByName('SupplierKey').AsInteger:=1;
          FieldByName('CategoryKey').AsInteger:=1;
          FieldByName('Unit').AsString:=trim(im_cb_unit.Text);
          FieldByName('Price').AsFloat:=im_edtPrice.Value;
          Post;
     end;
   MainPageRequestEntry.ActivePageIndex:=0;
   QryItem.Requery;
   btnAddtoItem.Visible:=False;
   spinItemSelect.SetFocus;
   QryItem.Locate('Name',trim(im_edtName.Text),[]);
   edtActiveItem.Text:=trim(QryItem.fieldbyname('Name').AsString);
end;

procedure TfrmRequestEntry.spinItemSelectDownLeftClick(Sender: TObject);
begin
   QryItem.Prior;
   edtActiveItem.Text:=trim(QryItem.fieldbyname('Name').AsString);
end;

procedure TfrmRequestEntry.spinItemSelectUpRightClick(Sender: TObject);
begin
   QryItem.Next;
   edtActiveItem.Text:=trim(QryItem.fieldbyname('Name').AsString);

end;

procedure TfrmRequestEntry.RzBitBtn6Click(Sender: TObject);
begin
     Close;
end;

procedure TfrmRequestEntry.RzBitBtn2Click(Sender: TObject);
var
i:integer;
begin
     with QryRequestDetails do
     begin
          Append;
          FieldByName('ITEMNAME').AsString:=trim(edtActiveItem.Text);
          FieldByName('REQUESTKEY').AsInteger:= QryRequest.FieldByName('REQUESTKEY').AsInteger;          
          Post;
     end;
     QryRequestDetails.Requery;
     QryRequestDetails.Filter:='REQUESTKEY=' + QryRequest.FieldByName('REQUESTKEY').AsString;
     QryRequestDetails.Filtered:=True;


with dbgridRequestItems do
   begin
        //ColCount:=20;
        for i:=1 to 10 do
        begin
                Fields.Add;
        end;

        DefaultColWidth:=50;
        FixedColWidth:=8;
        ColWidths[1]:=0;
        ColWidths[2]:=130;
        ColWidths[3]:=150;

        Fields[1].FieldName:= 'ITEMNAME';
        Fields[1].Title:=' Name ';
        Fields[1].Alignment:= taLeftJustify;

        Fields[2].FieldName:= 'DESCRIPTION';
        Fields[2].Title:=' Description ';
        Fields[2].Alignment:= taLeftJustify;

        Fields[3].FieldName:= 'QUANTITY';
        Fields[3].Title:='Quantity';
        Fields[3].Alignment:= taRightJustify;

        Fields[4].FieldName:= 'UNIT';
        Fields[4].Title:='Unit';
        Fields[4].Alignment:= taLeftJustify;

        Fields[5].FieldName:= 'UNITPRICE';
        Fields[5].Title:='Price';
        Fields[5].Alignment:= taRightJustify;

        Fields[6].FieldName:= 'REMARKS';
        Fields[6].Title:='Remarks';
        Fields[6].Alignment:= taLeftJustify;

end;
     dbgridRequestItems.Refresh;
     QryItem.Requery;
     QryItem.First;    
end;

procedure TfrmRequestEntry.btnAddtoItemClick(Sender: TObject);
begin
     MainPageRequestEntry.ActivePageIndex:=2;
     im_edtName.Text:=Trim(edtFindItem.Text);
     im_edtName.SetFocus;
end;

procedure TfrmRequestEntry.RzBitBtn3Click(Sender: TObject);
begin
     QryRequestDetails.Delete;
     QryRequestDetails.Requery;
     dbgridRequestItems.Refresh;

end;

procedure TfrmRequestEntry.rzBtnDTRSummaryClick(Sender: TObject);
begin
     QryRequest.Connection:=ADOConnection1;
     QryRequest.Open;
     QryRequest.Append;
     QryRequest.FieldByName('REQUESTKIND').AsString:='Normal';
     QryRequestDetails.Connection:=ADOConnection1;
     QryRequestDetails.Open;
     QryRequestDetails.Filter:='REQUESTKEY=0';// + QryRequest.FieldByName('REQUESTKEY').AsString;
     QryRequestDetails.Filtered:=True;

end;

procedure TfrmRequestEntry.RzBmpButton1Click(Sender: TObject);
begin
     QryRequest.Post;
end;

procedure TfrmRequestEntry.btnMainCloseClick(Sender: TObject);
begin
     close;
end;

end.
